package com.yami.shop.security.common.controller;

import com.anji.captcha.model.common.CaptchaTypeEnum;
import com.anji.captcha.model.common.Const;
import com.anji.captcha.model.common.RepCodeEnum;
import com.anji.captcha.model.common.ResponseModel;
import com.anji.captcha.model.vo.CaptchaVO;
import com.anji.captcha.service.CaptchaService;
import com.anji.captcha.service.impl.CaptchaServiceFactory;
import com.yami.shop.common.response.ServerResponseEntity;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Properties;

/**
 * @author FrozenWatermelon
 * @date 2020/7/30
 */
@RestController
@RequestMapping("/captcha")
@Tag(name = "验证码")
public class CaptchaController {

    private final CaptchaService captchaService;

    public CaptchaController(CaptchaService captchaService) {
        this.captchaService = captchaService;
    }

    @PostMapping({ "/get" })
    public ServerResponseEntity<ResponseModel> get(@RequestBody CaptchaVO captchaVO) {
        return ServerResponseEntity.success(captchaService.get(captchaVO));
    }

    @PostMapping({ "/getClickWord" })
    public ServerResponseEntity<ResponseModel> getClickWord(@RequestBody CaptchaVO captchaVO) {

        Properties config = new Properties();
        config.put(Const.CAPTCHA_CACHETYPE, "redis");
        config.put(Const.CAPTCHA_WATER_MARK, "");
        // 滑动验证
        config.put(Const.CAPTCHA_TYPE, CaptchaTypeEnum.CLICKWORD.getCodeValue());
        config.put("captcha.font.size", "30");
        config.put("captcha.font.color", "blue");
        config.put("captcha.font.name", "宋体");
//		config.put(Const.ORIGINAL_PATH_JIGSAW, FileUtil.getAbsolutePath("classpath:captcha"));
        CaptchaServiceFactory.getInstance(config);
        return ServerResponseEntity.success(CaptchaServiceFactory.getInstance(config).get(captchaVO));
    }

    @PostMapping({ "/check" })
    public ServerResponseEntity<ResponseModel> check(@RequestBody CaptchaVO captchaVO) {
        ResponseModel responseModel;
        try {
            responseModel = captchaService.check(captchaVO);
        }catch (Exception e) {
            return ServerResponseEntity.success(ResponseModel.errorMsg(RepCodeEnum.API_CAPTCHA_COORDINATE_ERROR));
        }
        return ServerResponseEntity.success(responseModel);
    }


    @PostMapping({ "/checkClickWord" })
    public ServerResponseEntity<ResponseModel> checkClickWord(@RequestBody CaptchaVO captchaVO) {
        ResponseModel model;
        try {
            Properties config = new Properties();
            config.put(Const.CAPTCHA_CACHETYPE, "redis");
            config.put(Const.CAPTCHA_WATER_MARK, "");
            // 滑动验证
            config.put(Const.CAPTCHA_TYPE, CaptchaTypeEnum.CLICKWORD.getCodeValue());
            config.put("captcha.font.size", "30");
            config.put("captcha.font.color", "blue");
            config.put("captcha.font.name", "宋体");
//		config.put(Const.ORIGINAL_PATH_JIGSAW, FileUtil.getAbsolutePath("classpath:captcha"));
            model = CaptchaServiceFactory.getInstance(config).check(captchaVO);
        }catch (Exception e) {
            return ServerResponseEntity.success(ResponseModel.errorMsg(RepCodeEnum.API_CAPTCHA_COORDINATE_ERROR));
        }
        return ServerResponseEntity.success(model);
    }

}
